;(function () {
  new Vue({
    el: '#yx-detail',
    data: {
      tabData:[
        {'name':'意向详情',},
        {'name':'凭证列表','privilege':'c323'},
        {'name':'相关文件'},
      ],
      tabSelectedIndex:0,
      loadIntentModule:true,
      loadVoucherModule:false,
      loadFileModule:false,
      id: hlUrlUtil.GetQueryString('id'),
      data: {zlfInfo: {},projectInfo:{stops:[]}},
      state: null,
      errData: '',
      refundMome: '',
      stateflag: false,
      refundflag: false,
      permissions: JSON.parse(window.localStorage.getItem('permissions')),
      tzhuanflag: false,
      contractFile: [],
      attachmentsList:'',
      progress:'',
      voucherList:[],
      projectId:'',
      refundDate:'',
    },
    filters: {
      dateTime (time) {
        if (!time) return '--';
        return hlDateUtil.formatUTCDate(time, 'yyyy-MM-dd HH:mm:ss')
      },
      dateTimeYYmmDD (time) {
        if (!time) return '--';
        return hlDateUtil.formatUTCDate(time, 'yyyy-MM-dd')
      }
    },
    computed: {
      unitList () {
        let result = [];
        if (this.data.intentType == 'ER') {
          let data = this.data.projectInfo.units;
          if (!data.length) return [];
          let buildinglist = hlArrayUtil.removeRepeat(data, 'buildingId')
          buildinglist.map(item => {
            result.push({
              projectName: this.data.projectInfo.projectName,
              projectId: this.data.projectInfo.projectId,
              buildingName: item.buildingName,
              buildingId: item.buildingId,
              floor: item.floorNum,
              floorId: item.floorId,
              children: []
            })
          })
          data.map(item => {
            result.map(item0 => {
              if (item.floorId == item0.floorId) {
                item0.children.push({unitName: item.unitName, unitId: item.unitId, acreage: item.unitAcreage})
              }
            })
          })
        }
        return result
      },
      wplist () {
        let result = [];
        if (this.data.intentType == 'WR') {
          let data = this.data.projectInfo.wps;
          if (!data.length) return [];
          let buildinglist = hlArrayUtil.removeRepeat(data, 'unitId')
          buildinglist.map(item => {
            result.push({
              projectName: this.data.projectInfo.projectName,
              projectId: this.data.projectInfo.projectId,
              buildingName: item.buildingName,
              buildingId: item.buildingId,
              floor: item.floorNum,
              floorNum: item.floorNum,
              floorId: item.floorId,
              unitId: item.unitId,
              unitName: item.unitName,
              children: []
            })
          })
          data.map(item => {
            result.map(item0 => {
              if (item.unitId == item0.unitId) {
                item0.children.push({wpName: item.wpName, wpId: item.wpId})
              }
            })
          })
        }
        return result
      },
      stlist () {
        let result = [];
        if (this.data.projectInfo.stops.length) {
          let data = this.data.projectInfo.stops;
          if (!data.length) return [];
          let buildinglist = hlArrayUtil.removeRepeat(data, 'unitId')
          buildinglist.map(item => {
            result.push({
              projectName: this.data.projectInfo.projectName,
              projectId: this.data.projectInfo.projectId,
              buildingName: item.buildingName,
              buildingId: item.buildingId,
              floor: item.floorNum,
              floorId: item.floorId,
              unitId: item.unitId,
              unitName: item.unitName,
              children: []
            })
          })
          data.map(item => {
            result.map(item0 => {
              if (item.unitId == item0.unitId) {
                item0.children.push({stopName: item.stopName, stopId: item.stopId})
              }
            })
          })
        }
        return result
      },
      stCount () {
        let count = 0;
        if (this.stlist.length) {
          this.stlist.map(item => {
            count += item.children.length
          })
        }
        return count
      },
      wpCount () {
        let count = 0;
        if (this.wplist.length) {
          this.wplist.map(item => {
            count += item.children.length
          })
        }
        return count
      },
      unitCount () {
        let count = 0;
        if (this.unitList.length) {
          this.unitList.map(item => {
            item.children.map(single =>{
              count += Number(single.acreage)
            })
          })
        }
        return count
      }
    },
    methods: {
      // 前往凭证详情
      goToVoucherDetail(id){
        hlUrlUtil.goTo('voucherDetail', { 'ddtab': true,'id':id }, true)
      },
      goToEdit(){
        hlUrlUtil.goTo('yxEdit', { 'ddtab': true,'id':this.id }, true)
      },
      // 获取财务凭证列表
      getVoucherList() {
        var that = this
        hlhttp.get({
          url: 'acct/voucher_info/_search',
          data: {
            "keyword":  '#',
            "voucher_type_name":  '#',
            "voucher_date_begin": '#',
            "voucher_date_end":  '#',
            "documenting_date_begin":  '#',
            "documenting_date_end":  '#',
            "export_flag": '#',
            "project_ids": this.projectId,
            "contract_id":  '#',
            "intent_id":this.id || '#',
            "page": 1,
            "limit": 1000000
          },
          success: function (res) {
            that.voucherList = res.acctVouchers
          }
        })
      },
      uploadFile: function () {
        if(this.progress){
          hl.toast('文件尚未上传完成')
          return
        }
        $('.upload-file-button').trigger('click')
      },
      // 删除文件
      deleteFile: function(val, i) {
        var self = this;
        hllayer.confirm1('此操作不可逆转，确定删除文件吗？', function () {
          hlhttp.delete({
            url: 'intent/attachment?intent_id=' + self.id + '&attachment_url=' + val,
            success: function() {
              hl.toast('删除成功')
              self.getDetailsData();
            }
          })
        })
      },
      previewImage:function(url){
        var imageExt = ['jpg', 'jpeg', 'png', 'JPG', 'JPEG', 'PNG'];
        var pdfExt = ['pdf', 'PDF'];
        var officeExt = ['docx','doc','xlsx','xls','xlt']
        var imageFlag = false;
        var pdfFlag = false;
        var officeFlag = false;

        imageExt.forEach((e, i) => {
          if (url.indexOf(e) !== -1) imageFlag = true;
        })

        if (imageFlag) {
          $.imageShow([url])
          return
        }
        pdfExt.forEach((e, i) => {
          if (url.indexOf(e) !== -1) pdfFlag = true;
        })
        if (pdfFlag) {
          $.previewPDF(url)
          return
        }

        officeExt.forEach((e, i) => {
          if (url.indexOf(e) !== -1) officeFlag = true
        })
        if (officeFlag) {
          $.previewOffice(url)
          return
        }
        hl.toast('暂不支持该格式')
      },
      getDetailsData: function () {
        var self = this;
        hlhttp.get({
          url: 'contract/intent',
          data: { 'id': self.id },
          success: (data) => {
            this.projectId = data.projectInfo.projectId
            this.attachmentsList = data.attachments
            if(this.permissions.indexOf('c323') != -1 ){
              this.getVoucherList();
            }
          }
        })
      },
      handleSelectedTab: function (val, i) {
        const {name,isDisabled} = val

        if (name == '意向详情') {
          this.loadIntentModule = true
          this.loadVoucherModule = false
          this.loadFileModule = false
          this.tabSelectedIndex = 0
        }
        if (name == '凭证列表') {
          this.loadIntentModule = false
          this.loadVoucherModule = true
          this.loadFileModule = false
          this.tabSelectedIndex = 1
        }
        if (name == '相关文件') {
          this.loadIntentModule = false
          this.loadVoucherModule = false
          this.loadFileModule = true
          this.tabSelectedIndex = 2
        }
      },
      //获取意向详情
      getYxInfo () {
        hlhttp.get({
          url: 'contract/intent',
          data: {id: this.id},
          success: (data) => {
            this.data = data;
          }
        })
      },
      ifYxBook () {
        hlhttp.put({
          url: 'contract/intent/check_confirm',
          data: {id: this.id},
          success: () => {
            this.state = 0;
            this.stateflag = true;
            this.getYxInfo()
          },
          error: (err) => {
            let res = JSON.parse(err.responseText);
            this.stateflag = true;
            if (res.code == 'INFO_WRONG') {
              this.state = 1;
            }
            if (res.code == 'UNIT_RENTED') {
              this.state = 2;
              this.errData = JSON.parse(res.msg);
            }
          }
        })
      },
      queryYx () {
        if (this.state > 0) {
          this.stateflag = false;
          return
        }
        hlhttp.put({
          url: 'contract/intent/_confirm',
          data: {id: this.id},
          success:() => {
            this.stateflag = false;
            this.getYxInfo()
          }
        })
      },
      hideYx () {
        this.stateflag = false;
      },
      cancelYx () {
        hllayer.confirm1('作废意向书时会同步作废意向金账单<br/>此操作不可能逆，确认作废意向书吗？', () => {
          hlhttp.put({
            url: 'contract/intent/_cancel',
            data: {id: this.id},
            success: () => {
              hl.toast('操作成功')
              this.getYxInfo()
            }
          })
        })
      },
      //显示退款框框
      refundShow() {
        this.refundflag = true;
      },
      //关闭退款框框
      refundHide () {
        this.refundflag = false;
        this.refundMome = '';
        this.refundDate = ''
      },
      //退款
      refundYx () {
        if (!this.refundDate) {
          hl.toast('请选择退款时间')
          return
        }
        hlhttp.put({
          url: 'contract/intent/_refund',
          data: {id: this.id, refund_reason: this.refundMome, refund_time: this.refundDate},
          success: () => {
            hl.toast('操作成功')
            this.getYxInfo()
            this.refundflag = false;
          }
        })
      },
      goContract () {
        hlhttp.get({
          url: 'contract/intent_checked',
          data: {intent_id: this.id},
          success: (res) => {
            if (res.enableToContract == 'Y') {
              var arr = [
                'CREATED_CONTRACT_FIRST_STEP',
                'CREATED_CONTRACT_SECOND_STEP',
                'RENT_BU_UNIT_MES',
                'RENT_ST_POSITION_MES',
                'RENT_WP_POSITION_MES',
                'RENT_UNIT_MES'
              ]
              hlStorage.ssClearMultipleKey(arr)
              hlUrlUtil.goTo('contractNew1', {intentId: this.id, ddtab: true}, true)
            }

            if (res.enableToContract == 'N') {
              this.tzhuanflag = true;
              setTimeout(() => {
                hlUrlUtil.goTo('contractNewDeatil', {id: res.contractId, version: res.version, ddtab: true}, true)
                this.tzhuanflag = false
              }, 2000)
            }
          }
        })

      }
    },
    created () {

    },
    mounted () {
      var self = this;
      this.getYxInfo()
      this.getDetailsData();

      fileUpload(
        '.upload-file-button', uploadUrl,
        function (res) {
          var projectId = hlUrlUtil.GetQueryString('projectId');
          self.progress = ''
          hlhttp.post({
            url: 'intent/attachment',
            data: {
              intent_id: self.id,
              attachment_name: res.json.name,
              attachment_url: res.json.url
            },
            success: function () {
              hl.toast('上传成功')
              self.getDetailsData();
            }
          })
        },
        function(val){
          self.progress = parseInt((val.loaded/val.total)*100) + '%'
        })

    }
  })
} ())
